package org.jackysoft.report.rows;

import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.jackysoft.entity.ArmItem;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.Product;
import org.jackysoft.entity.ProductType;
import org.jackysoft.report.DataItemType;
import org.jackysoft.report.DataItemUtil;
import org.jackysoft.service.ProductService;
import org.jackysoft.util.AbstractRowWatcher;
import org.jackysoft.util.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;

public class ArmItemResearchRowWatcher extends AbstractRowWatcher {
	static final Logger logger = LoggerFactory.getLogger(ArmItemResearchRowWatcher.class);
	private Collection<ArmItem> _arms;	
	private Department dept;
	private DataItemUtil dataItemUtil;	
	private ProductType _pType;

	public ArmItemResearchRowWatcher(
			Collection<ArmItem> arms,
			ProductService productService, 
			Department dept,
			DataItemUtil dataItemUtil,
			ProductType ptype) {
		super();
		this._arms = arms;
		if (_arms == null)_arms = Lists.newArrayList();			
		this.dept = dept;
		this.dataItemUtil = dataItemUtil;	
		
		this._pType = ptype;
	}

	@Override
	public void accept(Row row) {
		try {
			Iterator<Cell> cellItr = row.cellIterator();

			// 0 代码
			// 1 名称
			// 2 年初
			// 3 月初
			// 4 本月动工
			// 5.1 
			// 5 本月累计
			// 6 本月合格
			// 7 合格累计
			// 8 本月废品
			// 9 废品累计
			// 10 本月试销
			// 11 试销累计
			// 12 期末结存
			// 13 备注
			
			Cell code   	= ExcelUtil.nextCell(cellItr);//0	
			String cstr = ExcelUtil.cellString(code);
			if(StringUtils.isBlank(cstr))return;
			Cell name   	= ExcelUtil.nextCell(cellItr);//1
			Cell year   	= ExcelUtil.nextCell(cellItr);//2
			Cell month      = ExcelUtil.nextCell(cellItr);//3
			Cell action 	= ExcelUtil.nextCell(cellItr);// 4
			ExcelUtil.nextCell(cellItr);//跳过昨日动工累计列
			Cell actionAcl  = ExcelUtil.nextCell(cellItr);//5
			Cell pass 		= ExcelUtil.nextCell(cellItr);// 6
			ExcelUtil.nextCell(cellItr);//跳过昨日合格累计列
			Cell passAcl 	= ExcelUtil.nextCell(cellItr);//7
			Cell wast 		= ExcelUtil.nextCell(cellItr);//8
			ExcelUtil.nextCell(cellItr);//跳过昨日废品累计列
			Cell wastAcl 	= ExcelUtil.nextCell(cellItr);//9
			Cell test 		= ExcelUtil.nextCell(cellItr);//10
			ExcelUtil.nextCell(cellItr);//跳过昨日试销累计列
			Cell testAcl 	= ExcelUtil.nextCell(cellItr);//11
			Cell fstore 	= ExcelUtil.nextCell(cellItr);//12
			Cell remark     = ExcelUtil.nextCell(cellItr);//13
			ArmItem arm  = new ArmItem();
		    Product host = new Product();
		    host.setCode(ExcelUtil.cellString(code));
		    host.setName(ExcelUtil.cellString(name));
		    arm.setHost(host);
			arm.setDepartment(dept);
			arm.setRemark(ExcelUtil.cellString(remark));			
			arm.setItemType(this._pType);
			dataItemUtil.setDataItemValue(arm, DataItemType.ForwardStore, ExcelUtil.cellNumber(year),ExcelUtil.cellNumber(month));
			dataItemUtil.setDataItemValue(arm, DataItemType.Action,	ExcelUtil.cellNumber(action),ExcelUtil.cellNumber(actionAcl));
			dataItemUtil.setDataItemValue(arm, DataItemType.Passed,ExcelUtil.cellNumber(pass), ExcelUtil.cellNumber(passAcl));
			dataItemUtil.setDataItemValue(arm, DataItemType.Waster,	ExcelUtil.cellNumber(wast), ExcelUtil.cellNumber(wastAcl));
			dataItemUtil.setDataItemValue(arm, DataItemType.Test,ExcelUtil.cellNumber(test), ExcelUtil.cellNumber(testAcl));
			dataItemUtil.setDataItemValue(arm, DataItemType.FinalStore,ExcelUtil.cellNumber(fstore),0D);
			
			_arms.add(arm);
		} catch (NumberFormatException e) {
			logger.error("error for get line with ",e);
		} catch (IllegalStateException e){
			logger.error("error for get line with ",e);
		}

   }
}
